package com.smit.icastplus;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.os.IBinder;

import com.smit.icastplus.ssdp.SSDPInterface;
import com.smit.icastplus.ssdp.SSDPManager;
import com.smit.utils.LogUtil;

/**
 * icast plus app service
 */
public class ICastPlusAppService extends Service implements SSDPInterface {

    private static final String TAG = ICastPlusAppService.class.getSimpleName();
    private SSDPManager ssdpManager;
    private WifiManager.MulticastLock multicastLock;

    @Override
    public void onCreate() {
        super.onCreate();
        LogUtil.debug(TAG, "onCreate~~~");

        wifiMulticastUnLock();
        ssdpInit();
    }

    private void wifiMulticastUnLock() {
        WifiManager wm = (WifiManager) getSystemService(Context.WIFI_SERVICE);
        multicastLock = wm.createMulticastLock("multicastLock");
        multicastLock.acquire();
    }

    private void ssdpInit() {
        ssdpManager = SSDPManager.getInstance();
        ssdpManager.setCallback(this);
        ssdpManager.startSSDP();
    }

    @Override
    public void connectFail() {

    }

    @Override
    public void hostGet(String hostAddr) {

    }

    @Override
    public void hostLimit() {

    }

    @Override
    public IBinder onBind(Intent intent) {
        LogUtil.debug(TAG, "onBind~~~");
        return null;
    }

    @Override
    public void onDestroy() {
        LogUtil.debug(TAG, "ondestory~~~~~~~~~~~~~");

        if (ssdpManager != null) {
            ssdpManager.releaseResource();
            ssdpManager = null;
        }

        if (multicastLock != null) {
            multicastLock.release();
            multicastLock = null;
        }

        super.onDestroy();
    }
}